import pygame
from pytmx import load_pygame, TiledObjectGroup

from actor.japanese_soldier import JapaneseWalkSoldier
from actor.things_show_image import Prize
from actor.red_army_soldier import RedArmyShootSoldier
from actor.xiao_tie import XiaoTie
from game import ScenePassStatus
from scene import BaseScene
from scene.task.stage_one import TaskStatus
from scene.task.stage_one.task1_take_prizes import TakePrizesTask
from scene.task.stage_one.task2_kill_japanese import KillJapaneseTask
from scene.task.stage_one.task3_transmit_message import TransmitMessageTask
from scene.task.stage_one.task4_kill_japanese import KillJapaneseTaskAll

"""
地道战场景类
"""
class TunnelWarScene(BaseScene):
    def __init__(self, xiao_tie: XiaoTie):
        """
        场景初始化
        :param xiao_tie: 小铁对象
        """
        super(TunnelWarScene, self).__init__(xiao_tie,
                                             "./resources/tmx/tunnel_map.png")
        self.japanese_group = pygame.sprite.Group()
        self.prizes_group = pygame.sprite.Group()
        self.obstacle_group = pygame.sprite.Group()
        self.tiled_map_data = load_pygame("resources/tmx/tunnel_map_1.tmx")
        self.jian_jun = None
        self.tie_tou = None
        self.tasks = []
        self.task_id = 0
        self.init_groups()
        self.init_task()

    def draw_actor(self):
        """
        角色道具绘制
        :return:
        """
        self.xiao_tie.draw(self.current_surface, self.view_x, self.view_y)
        for obj in self.japanese_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)

        for obj in self.prizes_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)

        self.tasks[self.task_id].draw(self.current_surface, self.view_x, self.view_y)

    def init_groups(self):
        """
        初始化场景中的人物
        """
        for group in self.tiled_map_data.objectgroups:
            if isinstance(group, TiledObjectGroup):
                if group.name == '日本人':
                    for obj in group:
                        japanese = JapaneseWalkSoldier(obj.x, obj.y)
                        self.japanese_group.add(japanese)
                elif group.name == '奖品':
                    for obj in group:
                        prizes = Prize(obj.x, obj.y)
                        prizes.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.prizes_group.add(prizes)
                elif group.name == '障碍物':
                    for obj in group:
                        obs = pygame.sprite.Sprite()
                        obs.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obs)
                elif group.name == '解放军':
                    for obj in group:
                        if obj.name == '建军':
                            self.jian_jun = RedArmyShootSoldier(obj.x, obj.y)
                        if obj.name == '铁头':
                            self.tie_tou = RedArmyShootSoldier(obj.x, obj.y)
                elif group.name == '小铁':
                    for obj in group:
                        self.xiao_tie.set_pos(obj.x, obj.y)
                        self.xiao_tie.set_count(0)

    def init_task(self):
        """
        任务初始化
        :return:
        """
        self.tasks.append(TakePrizesTask(self.xiao_tie, self.japanese_group, self.prizes_group, self.font))
        self.tasks.append(KillJapaneseTask(self.xiao_tie, self.japanese_group, self.font))
        self.tasks.append(TransmitMessageTask(self.xiao_tie, self.jian_jun, self.tie_tou, self.japanese_group, self.font))
        self.tasks.append(KillJapaneseTaskAll(self.xiao_tie, self.japanese_group, self.font))

    def run(self, key_down_flag, key_list):
        """
        场景角色道具移动
        :param key_down_flag:
        :param key_list:
        :return:
        """
        self.xiao_tie.run(key_down_flag, key_list, self.obstacle_group)
        for japanese in self.japanese_group:
            japanese.run(None, None, self.obstacle_group)
        self.tasks[self.task_id].do_task(key_down_flag, key_list)
        task_status = self.tasks[self.task_id].get_status()
        # 如果胜利窗口显示完毕
        if task_status == TaskStatus.win_over:
            self.task_id += 1
            # 判断是否大于最大长度，如果是则胜利
            if self.task_id >= len(self.tasks):
                self.pass_status = ScenePassStatus.final_win
                pygame.mixer.music.stop()
        elif task_status == TaskStatus.fail_over:
            self.pass_status = ScenePassStatus.final_fail
            pygame.mixer.music.stop()

    def get_pass_status(self):
        """
        获取游戏状态
        :return:
        """
        if self.pass_status == ScenePassStatus.final_win:
            return self.pass_status, "FinalScene"
        elif self.pass_status == ScenePassStatus.final_fail:
            return self.pass_status, "FailScene"
        else:
            return self.pass_status, None
